-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rails 5.0.0beta2: Weird behaviour of config belongs_to_required_by_default #23589
Comments
Just find that installing friendly_id, only adding it in gemfile, was the reason of this issue. |
Rails 5.0.0.rc1, same issue, no friendly_id |
This is due to initialization order. Any plugin that loads AR will cause it to pull config to AR::Base so subsequent initializers will be too late in the game to take effect. Try setting this in config/application.rb instead or setting |
@jeremy thanks! But default rails initializer should be changed accordingly then so it won't confuse people in this situation |
Agree @sandfoxme. Up for submitting a PR? |
@jeremy ok, maybe tomorrow |
Actually I'm confused on how to fix this. Just changing initializer to p.s. and the issue should be reopened |
This issue bit me today after upgrading to RC2. Setting |
In my case, specifically, it seems like the Devise initializer caused the issue. Moving it to the bottom of the order ( |
While I agree that we can fix this issue it is more a plugin/gem not playing nice with the rails hooks. I'll take care of fixing it in devise and if you can try to find which gem/plugin is doing it please open an issue. |
Also ran into this issue (I think its this issue) when installing the Narrowed it down to the fact that it is re-opening ActiveRecord::Base. I suppose the question is where should this issue be fixed? Should gems not being re-opening ActiveRecord::Base? Should this be fixed in Rails? I only picked this up because my specs for You can reproduce this behaviour by simply re-opening ActiveRecord after loading Rails but before loading gems: # application.rb
require 'rails/all'
class ActiveRecord::Base
end
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups) |
My current preferred workaround for this issue, if caused by a gem, is to disable auto-require in your Gemfile: gem 'paranoia', require: false Create a new initialiser in which you require any libraries that re-open ActiveRecord # z_paranoia.rb
require 'paranoia' This ensures that it gets loaded after the configuration is set. |
This should be fixed in the gems. There is no problem in re-opening See heartcombo/devise@c2c74b0 on how they should be doing this. |
Many gems re-open ActiveRecord::Base so I think there should be at least bad behavior detection with a warning, otherwise we have incorrect state without any clear sign of it |
Should we not refer to model classes in initializers (to reference constants, e.g.)? Doing so in an early initializer seems to break active_record_belongs_to_required_by_default as well. |
It is not recommended, no. This not just breaking the hooks but also can connect to database when you should not be connecting. |
👍. This is something that we should have. If you want please submit a pull request. I'm closing since this is no much we can do here. |
Reposting my comment from #27844 just in case anyone here might find it helpful: If anyone else is having this same issue, I "fixed" it by removing the Really, this fixes the symptom rather than the problem - I'm not sure what's causing the initializer to not be loaded probably; it's probably one of my gems. But the above hack is good enough for now. |
Use AS.on_load to avoid setting AR consts before config loaded. It fixes `belongs_to_required_by_default` config behaviour disscussed in rails/rails#23589
It is not really clear what settings are changed… But an assumption would be: Coursemology/coursemology2#2632 They introduced it in here: rails/rails#23589
ActiveRecord should not be loaded in other gems or initializers. Instead we should use the on_load hook. rails/rails#23589
To find out which gem is calling ::ActiveRecord = 1 |
To figure out where issue comes from, better follow advice in #39855 (comment) and similar for ActiveJob #37030 (comment) Basically |
For avoiding autoloading these constants too soon. This affect the initialization of Active Record. Ref: rails/rails#23589 (comment)
I'm facing a weird issue with the new config belongs_to_required_by_default
In first app
created with rails 5.0.0beta1 and updated to 5.0.0beta2, I have in initializers/active_record_belongs_to_required_by_default.rb:
Rails.application.config.active_record.belongs_to_required_by_default = true
In rails console:
If I replace
Rails.application.config.active_record.belongs_to_required_by_default = true
withActiveRecord::Base.belongs_to_required_by_default = true
, all work as expected and I get my associations validated.After the replacement, I get in console:
In second app
freshly created with rails 5.0.0beta2, with
Rails.application.config.active_record.belongs_to_required_by_default = true
.In rails console:
The text was updated successfully, but these errors were encountered: